/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Codigo;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Time;
import java.text.SimpleDateFormat;
import javax.swing.ImageIcon;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableCellRenderer;

/**
 *
 * @author kyerte
 */
public class FrmPrincipal extends javax.swing.JFrame {

    static final String CONTROLADOR = "com.mysql.jdbc.Driver";
    public static Connection conexion;
    ModeloTablaCitas modeloTabla;

    /**
     * Creates new form FrmPrincipal
     */
    public FrmPrincipal() {
        this.setIconImage(new ImageIcon(getClass().getResource("/Imagenes/CRM.png")).getImage());
        initComponents();

//        try {
//
//            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
//
//        } catch (Exception e) {
//
//            e.printStackTrace();
//        }

        try {
            Class.forName(CONTROLADOR);
            conexion = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/EggoCRM", "root", "o5jkidyt");
        } catch (SQLException | ClassNotFoundException ex) {
            ex.printStackTrace();
        }

        if (conexion != null) {
            System.out.println("Conexión Establecida");
        } else {
            System.out.println("Conexión No Establecida");
        }

        modeloTabla = new ModeloTablaCitas();
        tablaCitas.setModel(modeloTabla);
        tablaCitas.setDefaultRenderer(Time.class, new RendererTiempo());
        acomodarColumnas();
    }

    FrmPrincipal(String usuario, Connection conex, boolean admin) {
        super("EGGO CRM " + usuario);
        this.setIconImage(new ImageIcon(getClass().getResource("/Imagenes/CRM.png")).getImage());
        conexion = conex;
        initComponents();
        lblUsuario.setText("Usuario: " + usuario);
        modeloTabla = new ModeloTablaCitas();
        tablaCitas.setModel(modeloTabla);
        tablaCitas.setDefaultRenderer(Time.class, new RendererTiempo());
        acomodarColumnas();

    }

    private void acomodarColumnas() {
        tablaCitas.getColumnModel().getColumn(0).setPreferredWidth(1);
        tablaCitas.getColumnModel().getColumn(1).setPreferredWidth(200);
        tablaCitas.getColumnModel().getColumn(2).setPreferredWidth(200);
        tablaCitas.getColumnModel().getColumn(3).setPreferredWidth(50);
        tablaCitas.getColumnModel().getColumn(4).setPreferredWidth(5);
        tablaCitas.getColumnModel().getColumn(5).setPreferredWidth(5);
        tablaCitas.getColumnModel().getColumn(6).setPreferredWidth(5);

    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        PanelBotones = new javax.swing.JPanel();
        cmdClientes = new javax.swing.JButton();
        cmdCitas = new javax.swing.JButton();
        cmdCasas = new javax.swing.JButton();
        cmdReportes = new javax.swing.JButton();
        cmdOpciones = new javax.swing.JButton();
        lblUsuario = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        cmdAsistente = new javax.swing.JButton();
        cmdBirthdays = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        tablaCitas = new javax.swing.JTable();
        cmdCitasSemana = new javax.swing.JButton();
        cmdCitasHoy = new javax.swing.JButton();
        lblCitas = new javax.swing.JLabel();
        cmdMailing = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Eggo CRM");

        cmdClientes.setFont(new java.awt.Font("Ubuntu", 2, 15)); // NOI18N
        cmdClientes.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/Clients.png"))); // NOI18N
        cmdClientes.setMnemonic('c');
        cmdClientes.setText("Clientes");
        cmdClientes.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmdClientesActionPerformed(evt);
            }
        });

        cmdCitas.setFont(new java.awt.Font("Ubuntu", 2, 15)); // NOI18N
        cmdCitas.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/Citas35.png"))); // NOI18N
        cmdCitas.setMnemonic('i');
        cmdCitas.setText("Citas");
        cmdCitas.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmdCitasActionPerformed(evt);
            }
        });

        cmdCasas.setFont(new java.awt.Font("Ubuntu", 2, 15)); // NOI18N
        cmdCasas.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/Tipos.png"))); // NOI18N
        cmdCasas.setMnemonic('b');
        cmdCasas.setText("Inmuebles");
        cmdCasas.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmdCasasActionPerformed(evt);
            }
        });

        cmdReportes.setFont(new java.awt.Font("Ubuntu", 2, 15)); // NOI18N
        cmdReportes.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/Reportes.png"))); // NOI18N
        cmdReportes.setMnemonic('r');
        cmdReportes.setText("Reportes");
        cmdReportes.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmdReportesActionPerformed(evt);
            }
        });

        cmdOpciones.setFont(new java.awt.Font("Ubuntu", 2, 15)); // NOI18N
        cmdOpciones.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/Opciones.png"))); // NOI18N
        cmdOpciones.setMnemonic('o');
        cmdOpciones.setText("Opciones");
        cmdOpciones.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmdOpcionesActionPerformed(evt);
            }
        });

        lblUsuario.setFont(new java.awt.Font("Ubuntu", 3, 18)); // NOI18N
        lblUsuario.setHorizontalAlignment(javax.swing.SwingConstants.TRAILING);
        lblUsuario.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/Usuarios.png"))); // NOI18N
        lblUsuario.setText("Usuario");

        cmdAsistente.setFont(new java.awt.Font("Ubuntu", 1, 15)); // NOI18N
        cmdAsistente.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/Accion35.png"))); // NOI18N
        cmdAsistente.setMnemonic('A');
        cmdAsistente.setText("Asistente de captura");
        cmdAsistente.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmdAsistenteActionPerformed(evt);
            }
        });

        cmdBirthdays.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/Balloons35.png"))); // NOI18N
        cmdBirthdays.setMnemonic('v');
        cmdBirthdays.setText("Ver cumpleaños");
        cmdBirthdays.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmdBirthdaysActionPerformed(evt);
            }
        });

        tablaCitas.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null}
            },
            new String [] {
                "idCita", "Cliente", "Promotor", "Lugar", "Fecha", "Hora", "Tipo"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false, false, true
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane1.setViewportView(tablaCitas);

        cmdCitasSemana.setMnemonic('t');
        cmdCitasSemana.setText("Citas esta semana");
        cmdCitasSemana.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmdCitasSemanaActionPerformed(evt);
            }
        });

        cmdCitasHoy.setMnemonic('h');
        cmdCitasHoy.setText("Citas hoy");
        cmdCitasHoy.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmdCitasHoyActionPerformed(evt);
            }
        });

        lblCitas.setFont(new java.awt.Font("Tahoma", 3, 24)); // NOI18N
        lblCitas.setText("Citas esta semana");

        cmdMailing.setFont(new java.awt.Font("Ubuntu", 2, 15)); // NOI18N
        cmdMailing.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/email_icon_32.png"))); // NOI18N
        cmdMailing.setMnemonic('M');
        cmdMailing.setText("Mailing");
        cmdMailing.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmdMailingActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout PanelBotonesLayout = new javax.swing.GroupLayout(PanelBotones);
        PanelBotones.setLayout(PanelBotonesLayout);
        PanelBotonesLayout.setHorizontalGroup(
            PanelBotonesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jSeparator1)
            .addGroup(PanelBotonesLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(PanelBotonesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(PanelBotonesLayout.createSequentialGroup()
                        .addComponent(lblCitas, javax.swing.GroupLayout.PREFERRED_SIZE, 408, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addGroup(PanelBotonesLayout.createSequentialGroup()
                        .addComponent(cmdClientes)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(cmdCitas, javax.swing.GroupLayout.PREFERRED_SIZE, 111, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(12, 12, 12)
                        .addComponent(cmdCasas, javax.swing.GroupLayout.PREFERRED_SIZE, 160, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(cmdReportes)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(cmdMailing)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(cmdOpciones)
                        .addGap(208, 208, 208)
                        .addComponent(lblUsuario, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(PanelBotonesLayout.createSequentialGroup()
                        .addGroup(PanelBotonesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(PanelBotonesLayout.createSequentialGroup()
                                .addComponent(cmdBirthdays, javax.swing.GroupLayout.PREFERRED_SIZE, 243, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(783, 783, 783)
                                .addComponent(cmdAsistente, javax.swing.GroupLayout.PREFERRED_SIZE, 243, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(0, 0, Short.MAX_VALUE))
                            .addGroup(PanelBotonesLayout.createSequentialGroup()
                                .addComponent(jScrollPane1)
                                .addGap(18, 18, 18)
                                .addGroup(PanelBotonesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(cmdCitasSemana, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 194, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(cmdCitasHoy, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 194, javax.swing.GroupLayout.PREFERRED_SIZE))))
                        .addContainerGap())))
        );
        PanelBotonesLayout.setVerticalGroup(
            PanelBotonesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(PanelBotonesLayout.createSequentialGroup()
                .addGap(16, 16, 16)
                .addGroup(PanelBotonesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cmdClientes, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(cmdCitas, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(cmdCasas, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(cmdReportes, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(cmdOpciones, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(lblUsuario)
                    .addComponent(cmdMailing, javax.swing.GroupLayout.DEFAULT_SIZE, 48, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lblCitas, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(PanelBotonesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(PanelBotonesLayout.createSequentialGroup()
                        .addComponent(cmdCitasHoy, javax.swing.GroupLayout.PREFERRED_SIZE, 55, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(cmdCitasSemana, javax.swing.GroupLayout.PREFERRED_SIZE, 55, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 267, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(47, 47, 47)
                .addGroup(PanelBotonesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cmdBirthdays, javax.swing.GroupLayout.PREFERRED_SIZE, 43, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cmdAsistente, javax.swing.GroupLayout.PREFERRED_SIZE, 43, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(PanelBotones, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(PanelBotones, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void cmdCitasActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdCitasActionPerformed
        FrmCitas ventanaCitas = new FrmCitas();
        ventanaCitas.setLocationRelativeTo(null);
        ventanaCitas.setVisible(true);
    }//GEN-LAST:event_cmdCitasActionPerformed

    private void cmdOpcionesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdOpcionesActionPerformed
        FrmOpciones ventanaOpciones = new FrmOpciones();
        ventanaOpciones.setLocationRelativeTo(null);
        ventanaOpciones.setVisible(true);
    }//GEN-LAST:event_cmdOpcionesActionPerformed

    private void cmdClientesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdClientesActionPerformed
        FrmClientes ventanaClientes = new FrmClientes();
        ventanaClientes.setLocationRelativeTo(null);
        ventanaClientes.setVisible(true);
    }//GEN-LAST:event_cmdClientesActionPerformed

    private void cmdCasasActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdCasasActionPerformed

        FrmDecision ventanaDecision = new FrmDecision();
        ventanaDecision.setLocationRelativeTo(null);
        ventanaDecision.setVisible(true);

    }//GEN-LAST:event_cmdCasasActionPerformed

    private void cmdAsistenteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdAsistenteActionPerformed
        Asistente asistenteCaptura = new Asistente();
        asistenteCaptura.setResizable(false);
        asistenteCaptura.setSize(700, 500);
        asistenteCaptura.setLocationRelativeTo(null);
        asistenteCaptura.setVisible(true);
    }//GEN-LAST:event_cmdAsistenteActionPerformed

    private void cmdBirthdaysActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdBirthdaysActionPerformed
        FrmBirthdays ventanaCumpleanios = new FrmBirthdays();
        ventanaCumpleanios.setLocationRelativeTo(null);
        ventanaCumpleanios.setVisible(true);
    }//GEN-LAST:event_cmdBirthdaysActionPerformed

    private void cmdCitasSemanaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdCitasSemanaActionPerformed
        try {
            lblCitas.setText("Citas esta semana");
            modeloTabla.obtenerCitasSemana();
            acomodarColumnas();
        } catch (SQLException ex) {
            ex.printStackTrace();
        } catch (IllegalStateException ex) {
            ex.printStackTrace();
        }
    }//GEN-LAST:event_cmdCitasSemanaActionPerformed

    private void cmdCitasHoyActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdCitasHoyActionPerformed
        try {
            lblCitas.setText("Citas hoy");
            modeloTabla.obtenerCitasHoy();
            acomodarColumnas();
        } catch (SQLException ex) {
            ex.printStackTrace();
        } catch (IllegalStateException ex) {
            ex.printStackTrace();
        }
    }//GEN-LAST:event_cmdCitasHoyActionPerformed

    private void cmdMailingActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdMailingActionPerformed
        FrmMailing ventanaMailing = new FrmMailing();
        ventanaMailing.setLocationRelativeTo(null);
        ventanaMailing.setVisible(true);
    }//GEN-LAST:event_cmdMailingActionPerformed

    private void cmdReportesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdReportesActionPerformed
        FrmReportes ventanaReportes = new FrmReportes();
        ventanaReportes.setLocationRelativeTo(null);
        ventanaReportes.setVisible(true);
    }//GEN-LAST:event_cmdReportesActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel PanelBotones;
    private javax.swing.JButton cmdAsistente;
    private javax.swing.JButton cmdBirthdays;
    private javax.swing.JButton cmdCasas;
    private javax.swing.JButton cmdCitas;
    private javax.swing.JButton cmdCitasHoy;
    private javax.swing.JButton cmdCitasSemana;
    private javax.swing.JButton cmdClientes;
    private javax.swing.JButton cmdMailing;
    private javax.swing.JButton cmdOpciones;
    private javax.swing.JButton cmdReportes;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JLabel lblCitas;
    private javax.swing.JLabel lblUsuario;
    private javax.swing.JTable tablaCitas;
    // End of variables declaration//GEN-END:variables
}

class ModeloTablaCitas extends AbstractTableModel {

    private final String[] NOMBRE_COLUMNAS = {"idCita", "Cliente", "Promotor",
        "Lugar", "Fecha", "Hora", "Tipo"};
    private PreparedStatement obtenerCitasHoy;
    private PreparedStatement obtenerCitasSemana;
    private Connection conexion;
    private ResultSet conjuntoResultados;
    private ResultSetMetaData metaDatos;
    private int numeroDeFilas;
    private boolean conectadoABaseDatos = false;
    private String citasSemana = "SELECT Cita.idCita, Clientes.Nombre, Promotor.Nombre, "
            + "Cita.Lugar, Cita.Fecha, Cita.Hora, Cita.Tipo FROM Cita JOIN Clientes "
            + "JOIN Promotor WHERE Clientes.idCliente = Cita.idCliente AND "
            + "Promotor.idPromotor = Cita.idPromotor AND WEEK(Cita.Fecha,3) = WEEK(NOW(),3)"
            + " ORDER BY Cita.Fecha";
    private String citasHoy = "SELECT Cita.idCita, Clientes.Nombre, Promotor.Nombre, "
            + "Cita.Lugar, Cita.Fecha, Cita.Hora, Cita.Tipo FROM Cita JOIN Clientes "
            + "JOIN Promotor WHERE Clientes.idCliente = Cita.idCliente AND "
            + "Promotor.idPromotor = Cita.idPromotor AND Cita.Fecha = CURDATE()"
            + " ORDER BY Cita.Fecha";

    ModeloTablaCitas() {

        conexion = FrmPrincipal.conexion;
        conectadoABaseDatos = true;

        try {

            obtenerCitasHoy = conexion.prepareStatement(citasHoy);

            obtenerCitasSemana = conexion.prepareStatement(citasSemana);

            obtenerCitasSemana();

        } catch (SQLException ex) {
            ex.printStackTrace();
        }
    }

    public void obtenerCitasHoy() throws SQLException, IllegalStateException {

        if (!conectadoABaseDatos) {
            throw new IllegalStateException("No hay conexión a la base de datos");
        }

        conjuntoResultados = obtenerCitasHoy.executeQuery();

        metaDatos = conjuntoResultados.getMetaData();

        conjuntoResultados.last();
        numeroDeFilas = conjuntoResultados.getRow();

        fireTableStructureChanged();
    }

    public void obtenerCitasSemana() throws SQLException, IllegalStateException {

        if (!conectadoABaseDatos) {
            throw new IllegalStateException("No hay conexión a la base de datos");
        }

        conjuntoResultados = obtenerCitasSemana.executeQuery();

        metaDatos = conjuntoResultados.getMetaData();

        conjuntoResultados.last();
        numeroDeFilas = conjuntoResultados.getRow();

        fireTableStructureChanged();
    }

    @Override
    public Class getColumnClass(int columna) throws IllegalStateException {

        if (!conectadoABaseDatos) {
            throw new IllegalStateException("No hay conexión a la base de datos");
        }

        try {
            String nombreClase = metaDatos.getColumnClassName(columna + 1);

            return Class.forName(nombreClase);
        } catch (Exception excepcion) {
            excepcion.printStackTrace();
        }

        return Object.class;
    }

    @Override
    public int getColumnCount() throws IllegalStateException {

        if (!conectadoABaseDatos) {
            throw new IllegalStateException("No hay conexión a la base de datos");
        }

        try {
            return metaDatos.getColumnCount();
        } catch (SQLException excepcionSql) {
            excepcionSql.printStackTrace();
        }
        return 0;
    }

    @Override
    public String getColumnName(int columna) throws IllegalStateException {

        if (!conectadoABaseDatos) {
            throw new IllegalStateException("No hay conexión a la base de datos");
        }

        try {
            return NOMBRE_COLUMNAS[columna];

        } catch (Exception excepcion) {
            excepcion.printStackTrace();
        }

        return "";
    }

    @Override
    public int getRowCount() throws IllegalStateException {

        if (!conectadoABaseDatos) {
            throw new IllegalStateException("No hay conexión a la base de datos");
        }

        return numeroDeFilas;
    }

    @Override
    public Object getValueAt(int fila, int columna) throws IllegalStateException {

        if (!conectadoABaseDatos) {
            throw new IllegalStateException("No hay conexión a la base de datos");
        }

        try {
            conjuntoResultados.absolute(fila + 1);
            return conjuntoResultados.getObject(columna + 1);
        } catch (SQLException excepcionSql) {
            excepcionSql.printStackTrace();
        }

        return "";
    }
}

class RendererTiempo extends DefaultTableCellRenderer {

    SimpleDateFormat f = new SimpleDateFormat("HH:mm:ss");

    public RendererTiempo() {
        super();
    }

    @Override
    public void setValue(Object valor) {
        setText((valor == null) ? "" : f.format(valor));
    }
}
